from configparser import *
import sys, os
if hasattr(sys, 'frozen'):
    os.environ['PATH'] = sys._MEIPASS + ";" + os.environ['PATH']
from bot_log import *

logger = BotLogger(os.path.basename(__file__)).getLogger()


class Config(object):
    """
    controller配置项
    """
    def __init__(self, config_path=''):
        self.config_path = config_path
        if self.config_path != '':
            self.__parse()

    def __parse(self):
        config_parser = ConfigParser()
        config_parser.read(self.config_path, encoding='UTF-8')

        self.db_path = 'inv.db'
        # self.server_name = '发票管理.exe'

        try:
            self.db_path = config_parser.get('data', 'path')
            # self.server_name = config_parser.get('data', 'server_name')
        except (NoSectionError, NoOptionError) as e:
            logger.debug('The config file does not include the data path: ' + str(e))

        self.exe_path = ''
        # self.tenant_type = '0'
        try:
            self.exe_path = config_parser.get('path', 'exe_path')
            # self.tenant_type = config_parser.get('hro', 'tenant_type')
        except (NoSectionError, NoOptionError) as e:
            logger.error(str(e))
        logger.info('db exe_path is {exe_path}'.format(exe_path=self.exe_path))
        # logger.info('db tenant_type is {tenant_type}'.format(tenant_type=self.tenant_type))

        # self.ac_floder = ''
        # try:
        #     self.ac_floder = config_parser.get('ac_floder', 'path')
        # except (NoSectionError, NoOptionError) as e:
        #     logger.error(str(e))


        # self.delay = 600
        # self.mid_delay = 7200
        # try:
        #     self.delay = config_parser.get('delay_time', 'delay')
        #     self.mid_delay = config_parser.get('delay_time', 'mid_delay')
        # except (NoSectionError, NoOptionError) as e:
        #     logger.error(str(e))
        # logger.info('delay is: {delay}, mid_delay is: {mid_delay}'.format(delay=self.delay, mid_delay=self.mid_delay))


cfg = Config()


def configure(config_path):
    global cfg
    cfg = Config(config_path)
    return cfg


def get_cfg():
    return cfg
